package com.vlyman.utils;

import com.alibaba.fastjson.JSONObject;
import org.yaml.snakeyaml.Yaml;

/**
 * 类描述：
 *  yml配置读取
 * @author chenjs
 * @date 2018/12/27 17:03
 **/
public class ApplicationYaml {
    private static JSONObject properties;
    static {
        Yaml yaml = new Yaml();
        properties = yaml.loadAs(ApplicationYaml.class.getResourceAsStream("/application.yml"), JSONObject.class);
    }

    /**
     * 方法描述：
     *   获取配置(多层结构以.做分割)
     *  @author chenjs
     *  @param keys
     *  @return String
     *  @date 2018/12/27 17:32
     */
    public static String getValue(String keys) {
        String[] keyArr = keys.split("\\.");
        JSONObject middleObj = properties;
        for (int i = 0; i < keyArr.length; i++) {
            if (keyArr.length == i+1) {
                return middleObj.getString(keyArr[i]);
            }
            middleObj = middleObj.getJSONObject(keyArr[i]);
        }
        return "";
    }
}
